package com.yzc.android.common.push;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.yzc.android.network.base.KDJsonObjectRequest;
import com.yzc.android.network.request.PushRequestFactory;
import org.json.JSONObject;

/**
 * Created by 冯子杰(fengzijie@edaijia-inc.cn)
 * Date: 14-7-12
 */
public abstract class PushDeviceTokenBinder implements IPushDeviceTokenBinder {

    private PushDeviceTokenBinderListener mListener;
    private KDJsonObjectRequest mApiRequest;

    @Override
    public void uploadDeviceToken(String token, final PushDeviceTokenBinderListener listener) {
        mApiRequest = PushRequestFactory.uploadDeviceToken(token, new Listener<JSONObject>() {
            @Override
            public void onResponse(final JSONObject response) {
                if (listener != null) {
                    listener.onUploadDeviceTokenSuccess();
                }
            }
        }, new ErrorListener() {
            @Override
            public void onErrorResponse(final VolleyError error) {
                if (listener != null) {
                    listener.onUploadDeviceTokenFailed(error);
                }
            }
        });
    }

    @Override
    public void unbindDeviceToken(String token, final PushDeviceTokenBinderListener listener) {
        mApiRequest = PushRequestFactory.unbindDeviceToken(token, new Listener<JSONObject>() {
            @Override
            public void onResponse(final JSONObject response) {
                if (listener != null)
                {
                    listener.onUnbindDeviceTokenSuccess();
                }
            }
        }, new ErrorListener() {
            @Override
            public void onErrorResponse(final VolleyError error) {
                if (listener != null)
                {
                    listener.onUnbindDeviceTokenFailed(error);
                }
            }
        });
    }

    @Override
    public void cancelProcess() {
        if (mApiRequest != null) {
            mApiRequest.cancel();
            mApiRequest = null;
        }
    }

    @Override
    public PushDeviceTokenBinderListener getListener() {
        return mListener;
    }

    @Override
    public void setListener(PushDeviceTokenBinderListener listener) {
        mListener = listener;
    }
}
